#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 1000
#define MAX_HISTORY 10

/* 定义体检信息结构体 */
struct Checkup {
	int student_id;
	char name[50];
	char gender[10];
	char department[50];
	int age;
	float height;
	float weight;
	float chest_size;
	char date[20];
};

/* 定义病历信息结构体 */
struct MedicalRecord {
	int student_id;
	char name[50];
	char gender[10];
	char department[50];
	char diagnosis[100];
	char date[20];
};

/* 定义学生健康档案结构体 */
struct HealthRecord {
	struct Checkup checkup;  /* 体检信息 */
	struct MedicalRecord medical_record;  /* 病例信息 */
	float height_history[MAX_HISTORY];  /* 历史身高数据 */
	float weight_history[MAX_HISTORY];  /* 历史体重数据 */
	int num_height_history;  /* 历史身高记录数量 */
	int num_weight_history;  /* 历史体重记录数量 */
};

/* 学生健康档案数据库 */
struct HealthRecord health_records[MAX_STUDENTS];
int num_records = 0;

float CalculateAverageGrowthRate();
void UpdateHealthRecord();
/* 登记学生信息 */
void RegisterRecord() {
	// 实现省略，与原来的代码一样
	
	int num_students;
	
	printf("请输入录入学生的个数：");
	scanf("%d", &num_students);
	
	int i = 0;
	while (i < num_students){
		struct HealthRecord new_record; /*建立新的学生健康档案*/
		
		printf("请录入第%d位学生信息 \n", i + 1);
		i++;
		
		printf("请输入学号: ");
		scanf("%d", &new_record.checkup.student_id);
		printf("请输入姓名: ");
		scanf("%s", new_record.checkup.name);
		printf("请输入性别: ");
		scanf("%s", new_record.checkup.gender);
		printf("请输入系别: ");
		scanf("%s", new_record.checkup.department);
		printf("请输入年龄: ");
		scanf("%d", &new_record.checkup.age);
		printf("请输入身高: ");
		scanf("%f", &new_record.checkup.height);
		printf("请输入体重: ");
		scanf("%f", &new_record.checkup.weight);
		printf("请输入胸围: ");
		scanf("%f", &new_record.checkup.chest_size);
		printf("请输入体检日期: ");
		scanf("%s", new_record.checkup.date);
		
		printf("请输入诊断信息: ");
		scanf("%s", new_record.medical_record.diagnosis);
		printf("请输入病历日期: ");
		scanf("%s", new_record.medical_record.date);
		
		new_record.medical_record.student_id = new_record.checkup.student_id;
		strcpy(new_record.medical_record.name, new_record.checkup.name);
		strcpy(new_record.medical_record.gender, new_record.checkup.gender);
		strcpy(new_record.medical_record.department, new_record.checkup.department);
		
		health_records[num_records++] = new_record;
		
		printf("登记成功！\n");
		
		system("pause");
	}
}

/* 统计学生信息 */
void Statistics() {
	// 实现省略，与原来的代码一样
	
	printf("=== 基本健康状况统计 ===\n");
	printf("1. 平均身高: %.2f\n", CalculateAverageHeight());
	printf("2. 平均体重: %.2f\n", CalculateAverageWeight());
	printf("3. 平均年龄: %.2f\n", CalculateAverageAge());
	printf("4. 平均胸围: %.2f\n", CalculateAverageChestSize());
	
	
	// 计算平均年增长值和年增长率
	printf("7. 平均年增长值和年增长率\n");
	for (int i = 0; i < num_records; i++) {
		float height_growth_rate = CalculateAverageGrowthRate(health_records[i].height_history, health_records[i].num_height_history);
		float weight_growth_rate = CalculateAverageGrowthRate(health_records[i].weight_history, health_records[i].num_weight_history);
		printf("学号：%d, 平均年身高增长值：%.2f, 年增长率：%.2f%%\n",
			health_records[i].checkup.student_id,
			height_growth_rate,
			height_growth_rate);
		printf("学号：%d, 平均年体重增长值：%.2f, 年增长率：%.2f%%\n",
			health_records[i].checkup.student_id,
			weight_growth_rate,
			weight_growth_rate);
	}
}

/* 计算平均年增长值 */
float CalculateAverageGrowthRate(float *history, int num_history) {
	if (num_history < 2) {
		return -1; // 无法计算
	}
	
	float total_growth = 0;
	for (int i = 1; i < num_history; ++i) {
		total_growth += history[i] - history[i - 1];
	}
	return total_growth / (num_history - 1);
}

/* 更新学生健康记录 */
void UpdateHealthRecord(int student_id, float new_height, float new_weight) {
	// 找到对应学生的健康记录
	int index = -1;
	for (int i = 0; i < num_records; ++i) {
		if (health_records[i].checkup.student_id == student_id) {
			index = i;
			break;
		}
	}
	
	// 如果找到了对应学生的记录
	if (index != -1) {
		// 更新身高和体重历史记录
		struct HealthRecord *record = &health_records[index];
		record->height_history[record->num_height_history++] = new_height;
		record->weight_history[record->num_weight_history++] = new_weight;
	}
}

int main() {
	// 实现省略，与原来的代码一样
	int choice = 1;
	
	while (choice != 0)
	{
		
		system("CLS");
		printf("\n\t\t  学生健康档案管理系统");
		printf("\n\t\t========================");
		printf("\n\t\t\t 1.登记");
		printf("\n\t\t\t 2.修改");
		printf("\n\t\t\t 3.删除");
		printf("\n\t\t\t 4.查询");
		printf("\n\t\t\t 5.统计");
		printf("\n\t\t\t 6.保存");
		printf("\n\t\t\t 7.读取");
		printf("\n\t\t\t 0.退出");
		printf("\n\t\t========================");
		printf("\n\t\t  请输入选项：");
		scanf("%d", &choice);
		system("cls");
		
		switch (choice) {
		case 1:
			RegisterRecord();
			break;
		case 2:
			ModifyRecord();
			break;
		case 3:
			DeleteRecord();
			break;
		case 4:
			QueryRecord();
			break;
		case 5:
			Statistics();
			break;
		case 6:
			SaveRecordsToFiles();
			break;
		case 7:
			LoadRecordsFromFiles();
			break;
		case 0:
			printf("谢谢使用！\n");
			break;
		default:
			printf("无效选项，请重新输入。\n");
		}
	} 
	
	system("pause");
	return 0;
}

